package com.springcloud.sc.ucpv5.service.impl;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.springcloud.sc.ucp.v5.api.mongo.ucpdb.MediaInfoData;
import com.springcloud.sc.ucp.v5.api.mongo.ucpdb.PublicSmsStatus;
import com.springcloud.sc.ucpv5.service.PublicSmsStatusService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Slf4j
@Service
public class PublicSmsStatusServiceImpl implements PublicSmsStatusService {

    @Autowired
    @Qualifier(value = "ucpMongoTemplate")
    private MongoTemplate ucpMongoTemplate;

    @Override
    public List getPublicSmsStatusListApi(Map map) {

        List<PublicSmsStatus> publicSmsStatusList = null;
        String  mobile = null;
        if(map.get("mobile")!=null){
            mobile = String.valueOf(map.get("mobile"));
        }
        String  receivetime = null;
        if(map.get("receivetime")!=null){
            receivetime = String.valueOf(map.get("receivetime"));
        }
        String  uuid = null;
        if(map.get("uuid")!=null){
            uuid = String.valueOf(map.get("uuid"));
        }
        String  from = null;
        if(map.get("from")!=null){
            from = String.valueOf(map.get("from"));
        }
        String  taskID = null;
        if(map.get("taskID")!=null){
            taskID = String.valueOf(map.get("taskID"));
        }
        String  status = null;
        if(map.get("status")!=null){
            status = String.valueOf(map.get("status"));
        }
        String  sendtime = null;
        if(map.get("sendtime")!=null){
            sendtime = String.valueOf(map.get("sendtime"));
        }
        String  msgContent = null;
        if(map.get("msgContent")!=null){
            msgContent = String.valueOf(map.get("msgContent"));
        }

        String  startTime = null;
        if(map.get("startTime")!=null){
            startTime = String.valueOf(map.get("startTime"));
        }

        String  endTime = null;
        if(map.get("endTime")!=null){
            endTime = String.valueOf(map.get("endTime"));
        }
        String  index = null;
        if(map.get("index")!=null){
            index = String.valueOf(map.get("index"));
        }



        try {
            Criteria criteria = new Criteria();
            BasicDBList basicDBList=new BasicDBList();
            DBObject dbObject =new BasicDBObject();
            if (mobile!=null){

                basicDBList.add(new BasicDBObject("mobile","mobile"));
            }
            if (receivetime!=null){
                basicDBList.add(new BasicDBObject("receivetime",receivetime));
            }
            if (uuid!=null){

                basicDBList.add(new BasicDBObject("uuid",uuid));
            }

            if (from!=null){

                basicDBList.add(new BasicDBObject("from",from));
            }

            if (taskID!=null){
                basicDBList.add(new BasicDBObject("taskID",taskID));
            }

            if (status!=null){

                basicDBList.add(new BasicDBObject("status",status));
            }
            if (index!=null){
                basicDBList.add(new BasicDBObject("index",index));
            }

            if (sendtime!=null){
                basicDBList.add(new BasicDBObject("sendtime",sendtime));
            }

            if (msgContent!=null){
                basicDBList.add(new BasicDBObject("msgContent",msgContent));
            }
            dbObject.put("$and", basicDBList);

            if(startTime!=null && endTime!=null){
                dbObject.put("sendtime", (new BasicDBObject("$gte", startTime).append("$lte", endTime)));
            }

            //暂时屏蔽
           /* Query query=new BasicQuery(dbObject);
            publicSmsStatusList =   ucpMongoTemplate.find( query, PublicSmsStatus.class);*/

        } catch (Exception e) {
            log.error("getPblicSmsStatusListApi Exception");
            e.printStackTrace();
        }
        return publicSmsStatusList;
    }

    @Override
    public List getMediaListApi(Map map) {

        List<MediaInfoData> mediaInfoDataList = null;
        String  number = null;
        if(map.get("number")!=null){
            number = String.valueOf(map.get("number"));
        }
        String  startTime = null;
        if(map.get("startTime")!=null){
            startTime = String.valueOf(map.get("startTime"));
        }
        String  endTime = null;
        if(map.get("endTime")!=null){
            endTime = String.valueOf(map.get("endTime"));
        }
        String  callRefId = null;
        if(map.get("callRefId")!=null){
            callRefId = String.valueOf(map.get("callRefId"));
        }

        if(callRefId != null){
            // 单条件查询
            try {
                DBObject dbObject =new BasicDBObject();
                // wsy 20190617 callRefId 存储对象非字符串,转成长整形
                long callRefId_long = Long.parseLong(callRefId);
            dbObject.put("callref",callRefId_long);

                //暂时屏蔽
            /*Query query=new BasicQuery(dbObject);
            mediaInfoDataList = ucpMongoTemplate.find( query, MediaInfoData.class);*/

        } catch (Exception e) {
            log.error("getPblicSmsStatusListApi Exception");
            e.printStackTrace();
        }
    }
        else{
        // 号码模糊查询和时间查询
        // 组合查询条件
        BasicDBList condList = new BasicDBList();

        DBObject query1 = new BasicDBObject(); // setup the query criteria
        // 设置查询条件
        query1.put("storetime", (new BasicDBObject("$gte", startTime).append("$lte", endTime)));
        query1.put("playlen", (new BasicDBObject("$gt", 0)));

        condList.add(query1);

        if (number.equals("00000000000")) {
            ;
        } else {
            // 模糊匹配
            Pattern pattern = Pattern.compile("^.*" + number + ".*$", Pattern.CASE_INSENSITIVE);
            DBObject queryCondition = new BasicDBObject();
            BasicDBList values = new BasicDBList();
            values.add(new BasicDBObject("dn", pattern));
            values.add(new BasicDBObject("grpdn", pattern));
            // 对端号码
            // values.add(new BasicDBObject("oprdn", pattern));
            queryCondition.put("$or", values);

            BasicDBList orList = new BasicDBList();
            orList.add(queryCondition);
            query1.put("$and", orList);

        }

            //暂时屏蔽
       /* Query query=new BasicQuery(query1);
        mediaInfoDataList =   ucpMongoTemplate.find( query, MediaInfoData.class);*/
    }

        return mediaInfoDataList;
}
}
